package com.sfy.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sfy.auth.application.convert.AuthUserDTOConverter;
import com.sfy.auth.entity.AuthUserDTO;
import com.sfy.auth.common.entity.Result;
import com.sfy.auth.domain.entity.AuthUserBO;
import com.sfy.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@Param("validCode") String validCode) {
        try {
            Preconditions.checkNotNull(validCode, "验证码不能为空");
            return Result.OK(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.Fail(e);
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }


    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController:register:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getNickName(), "昵称不能为空");
            Preconditions.checkNotNull(authUserDTO.getEmail(), "邮箱不能为空");
            Preconditions.checkNotNull(authUserDTO.getPhone(), "手机不能为空");
            Preconditions.checkNotNull(authUserDTO.getSex(), "性别不能为空");
            Preconditions.checkNotNull(authUserDTO.getAvatar(), "头像不能为空");
            Preconditions.checkNotNull(authUserDTO.getIntroduce(), "自我介绍不能为空");
            Preconditions.checkNotNull(authUserDTO.getExtJson(), "扩展不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.authUserDTOToAuthUserBO(authUserDTO);
            Boolean register = authUserDomainService.register(authUserBO);
            return Result.OK(register);
        } catch (Exception e) {
            log.error("UserController.register.error:{}", e.getMessage(), e);
            return Result.Fail(e.getMessage());
        }
    }

    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController:update:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getNickName(), "昵称不能为空");
            Preconditions.checkNotNull(authUserDTO.getSex(), "性别不能为空");
            Preconditions.checkNotNull(authUserDTO.getEmail(), "邮箱不能为空");
            Preconditions.checkNotNull(authUserDTO.getPhone(), "手机不能为空");
            Preconditions.checkNotNull(authUserDTO.getIntroduce(), "自我介绍不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.authUserDTOToAuthUserBO(authUserDTO);
            Boolean isUpdate = authUserDomainService.update(authUserBO);
            return Result.OK(isUpdate);
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.Fail("更新失败");
        }
    }

    /**
     * 用户删除
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController:delete:{}", JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.authUserDTOToAuthUserBO(authUserDTO);
            Boolean isUpdate = authUserDomainService.delete(authUserBO);
            return Result.OK(isUpdate);
        } catch (Exception e) {
            log.error("UserController.delete.error:{}", e.getMessage(), e);
            return Result.Fail("更新失败");
        }
    }

    /**
     * 用户启用禁用
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController:changeStatus:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.authUserDTOToAuthUserBO(authUserDTO);
            return Result.OK(authUserDomainService.changes(authUserBO));
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.Fail("启用，禁用失败");
        }
    }

    /**
     * 获取用户信息
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController:getUserInfo:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.authUserDTOToAuthUserBO(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            AuthUserDTO res = AuthUserDTOConverter.INSTANCE.authUserBOToAuthUserDTO(userInfo);
            return Result.OK(res);
        } catch (Exception e) {
            log.error("UserController.getUserInfo.error:{}", e.getMessage(), e);
            return Result.Fail("启用，禁用失败");
        }
    }

    @RequestMapping("logOut")
    public Result<AuthUserDTO> logOut(@RequestParam String userName) {
        try {
            log.info("UserController:logOut:{}", JSON.toJSONString(userName));
            Preconditions.checkNotNull(userName, "用户名不能为空");
            StpUtil.logout(userName);
            return Result.OK();
        } catch (Exception e) {
            log.error("UserController.logOut.error:{}", e.getMessage(), e);
            return Result.Fail("登录失效");
        }
    }

}
